package mypackage;

import java.io.IOException;
import java.io.OutputStream;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.RecordControl;
import net.rim.device.api.media.control.AudioPathControl;

public class RecordClient extends Thread implements PlayerListener
{
    private Player recordP;
    
    private OutputStream recordOut = new OutputStream()
    {
        private long currentTime;
        private long prvTime;
        
        public void write(byte[] b, int off, int len) throws IOException
        {
            currentTime = System.currentTimeMillis();
            MyScreen.log("write" + " offset:" + off + " length:" + len
                + " time:" + (currentTime - prvTime) + " buffer address:" + b);
            prvTime = currentTime;
        }

        public void write(byte[] b) throws IOException
        {
            super.write(b);
        }
        
        public void write(int b) throws IOException
        {
            
        }

        public void close() throws IOException
        {
            super.close();
        }

        public void flush() throws IOException
        {
            super.flush();
        }
        
    };

    public void run()
    {
        try
        {
            recordP = Manager.createPlayer("capture://audio?encoding=pcm");
            recordP.addPlayerListener(this);
            recordP.realize();
            RecordControl recordControl = (RecordControl) recordP
                .getControl("RecordControl");
            recordControl.setRecordStream(recordOut);

            recordControl.startRecord();
            AudioPathControl lPathCtr = (AudioPathControl) recordP
                .getControl("net.rim.device.api.media.control.AudioPathControl");
            lPathCtr.setAudioPath(AudioPathControl.AUDIO_PATH_HANDSET);
            recordP.prefetch();
            recordP.start();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        catch (MediaException e)
        {
            e.printStackTrace();
        }
    }

    public void playerUpdate(Player player, String event, Object eventData)
    {
        
    }
}
